package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.TypePuce;
import visiopuce.objets.TypeQC;

public class TypeQCDAOImpl extends GenericDAOImpl<TypeQC, BigDecimal> implements TypeQCDAO {

	/** Constructeur priv� */
	private TypeQCDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static TypeQCDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static TypeQCDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new TypeQCDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public ArrayList<TypeQC> getTypeQCByTypePuce(TypePuce typePuce) {
		ArrayList<TypeQC>  types = null;
		String sql = "SELECT r FROM TypeQC r ";
		if (typePuce != null){
			sql +=" WHERE r.typePuce = :typePuce";
		}
		Query query = HibernateUtil.getSession().createQuery(sql);
		if (typePuce != null){
			query.setParameter("typePuce", typePuce);
		}
		types = (ArrayList<TypeQC>) findMany(query);
		return types;
	}

	@Override
	public TypeQC getTypeQCByTypePuceAndLibelle(TypePuce typePuce, String typeQCs) {
		TypeQC  type = null;
		String sql = "SELECT r FROM TypeQC r ";
		if (typePuce != null){
			sql +=" WHERE r.typePuce.idTypePuce = :typePuce and r.libelle  LIKE :typeQCs";
		}
		Query query = HibernateUtil.getSession().createQuery(sql);
		if (typePuce != null){
			query.setParameter("typePuce", typePuce.getIdTypePuce());
			query.setParameter("typeQCs", typeQCs+"%");
		}
		type =  findOne(query);
		return type;
	}

	
}